﻿/**
* @description  Combines multiple tweens and runs them simultaneously.
*/
package com.foed.animation {

	import com.foed.events.TweenEvent;

	public class TweenComposite extends Animator {
	
		private var _tweenIndex:uint;
		private var _tweens:Array;
	
		/**
		* @description  Constructor.
		*
		* @param  pTweens  A list of tweens to run.
		*/
		public function TweenComposite(pTweens:Array) {
			_tweens = pTweens;
		}
	
		/**
		* @description  Called each time a single tween completes.
		*
		* @param  pEvent  Event fired by Animator.
		*/
		private function onEndTween(pEvent:TweenEvent):void {
			if (++_tweenIndex >= _tweens.length) {
				dispatchEvent(new TweenEvent(TweenEvent.END, this));
			}
		}
	
		/**
		* @description  Stops the animation.
		*/
		override public function halt():void {
			for each (var i:Animator in _tweens) {
				i.removeEventListener(TweenEvent.END, onEndTween);
				i.halt();
			}
		}
	
		/**
		* @description  Starts the animation.
		*/
		override public function start():void {
			_tweenIndex = 0;
			for each (var i:Animator in _tweens) {
				i.addEventListener(TweenEvent.END, onEndTween);
				i.start();
			}
		}
			
		/**
		* @description  Cleans up any timers that might be running.
		*/
		override public function die():void {
			super.die();
			_tweens = null;
		}
		
	}
	
}